<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Config;

class Index extends Common
{
	/**
     * 首页
     * @param string $name 缓存标识
     * @return array
     */
    public function index()
    {
    	$list = \think\Db::table('d_home_image')
    		->order('id ASC')
    		->field('id, pic, pc_column, m_column')
    		->where('status', 1)
    		->select();

        $map_data = $this->common;

    	// dd($list);
        return view('index', compact('list', 'map_data'));
    }

    /**
     * about
     * @return array
     */
    public function about()
    {
    	$about = \think\Db::table('d_abouts')->order('id DESC')->find();
        $map_data = $this->common;

    	return view('about', compact('about', 'map_data'));
    }

    /**
     * Store Images
     * @return array
     */
    public function store()
    {
        //dads
    	$list = \think\Db::table('d_store_type')->field('id, pic')->select();
    	foreach ($list as $key => $value) {
    		$cedl = \think\Db::table('d_store_image')->where('type_id', $value['id'])->select();
    		$list[$key]['som'] = $cedl;
    	}
    	// dump($list);

        $map_data = $this->common;

    	return view('store', compact('list', 'map_data'));
    }

    /**
     * Campaign Images
     * @return array
     */
    public function campaign()
    {
        $type_id = ''; 

        $banner_pic = Config::get('CAMPAIGN_BANNER');
        // dd($banner_pic);

        $map_where = [];
        if (!empty($this->request->param('type_id'))) {
            $map_where['type_id'] = $this->request->param('type_id');
            $type_id = $this->request->param('type_id');

            $banner_pic = \think\Db::table('d_campadign_type')->where('id', $type_id)->value('pic');
            // dd($banner_pic);
            if(empty($banner_pic)){
                $banner_pic = Config::get('CAMPAIGN_BANNER');
            }
        }

        $list = \think\Db::table('d_campadign')
            ->where($map_where)
            ->order('id DESC')
            ->limit(4)
            ->select();

        $map_data = $this->common;

         //求出总条数
        $count =  \think\Db::table('d_campadign')->where($map_where)->count();
        //每页显示多少条
        $length = 4;
        //总共多少页
        $num_page = ceil($count/$length);


        // dd($list);

    	return view('campaign', compact('map_data', 'list', 'type_id', 'num_page', 'banner_pic'));
    }

    /**
     * New Collection
     * @return array
     */
    public function collection()
    {
        $map_data = $this->common;
        $list = \think\Db::table('d_collections')->order('id DESC')->limit(4)->select();
        // dd($list);

         //求出总条数
        $count =  \think\Db::table('d_collections')->count();
        //每页显示多少条
        $length = 4;
        //总共多少页
        $num_page = ceil($count/$length);

        // dd($num_page);

    	return view('collection', compact('list', 'map_data', 'num_page'));
    }

    public function ajaxs_camp()
    {
        $map = [];
        //接收当前页
        $p = Request::instance()->param('page');
        $type_id = Request::instance()->param('type_id');
        if($type_id > 0 && !empty($type_id)){
            $map['type_id'] = $type_id;
        }

        $page = empty($p)?1:$p;
        //求出总条数
        $count =  \think\Db::table('d_campadign')->where($map)->count();
        //每页显示多少条
        $length = 4;
        //总共多少页
        $num_page = ceil($count/$length);
        //求出偏移量
        $limit = ($page-1)*$length;
        //查询
        $list =  \think\Db::table('d_campadign')->where($map)->limit($limit, $length)->order('id DESC')->select();

        $data['list'] = $list;
        $data['type_id'] = $type_id;    //当前分类的id

        return success($data, 'ok');
    }

    //collection ajax
    public function ajaxs()
    {
        //接收当前页
        $p = Request::instance()->param('page');

        $page = empty($p)?1:$p;
        //求出总条数
        $count =  \think\Db::table('d_collections')->count();
        //每页显示多少条
        $length = 4;
        //总共多少页
        $num_page = ceil($count/$length);
        //求出偏移量
        $limit = ($page-1)*$length;
        //查询
        $data['list'] =  \think\Db::table('d_collections')->limit($limit, $length)->order('id DESC')->select();
        $data['num_page'] = $num_page;

        // dump($data);exit;

        return success($data, 'ok');
    }
}
